package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo(strategyDetailMapper.selectForList(qo));
    }

    @Override
    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        if (id == null) {
            return null;
        }
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(StrategyDetail detail, String tags) {

        if (detail == null) {
            throw new LogicException("参数错误");
        }
        // isabroad
        Strategy stg = detail.getStrategy();

        if (stg == null || stg.getId() == null) {
            throw new LogicException("参数错误");
        }

        StrategyCatalog catalog = detail.getCatalog();
        if (catalog == null || catalog.getId() == null) {
            throw new LogicException("参数错误");
        }

        StrategyContent strategyContent = detail.getStrategyContent();
        if (strategyContent == null || strategyContent.getContent() == null) {
            throw new LogicException("参数错误");
        }
        // summary, 截取攻略内容的前200个字符
        String text = strategyContent.getContent();
        detail.setSummary(text.length() > 200 ? text.substring(0, 200) : text);

        Strategy strategy = strategyService.get(stg.getId());
        if (strategy == null || strategy.getDest() == null) {
            throw new LogicException("参数错误");
        }
        List<Destination> toasts = destinationService.getToasts(strategy.getDest().getId());
        if (toasts == null || toasts.size() == 0) {
            throw new LogicException("参数错误");
        }
        detail.setIsabroad(toasts.get(0).getId() != 1);
        // dest
        detail.setDest(toasts.get(toasts.size() - 1));
        if (detail.getId() == null) {
            // createTime
            detail.setCreateTime(new Date());
            strategyDetailMapper.insert(detail);
            strategyContent.setId(detail.getId());
            strategyContentMapper.insert(strategyContent);
        } else {
            strategyDetailMapper.updateByPrimaryKey(detail);
            strategyContent.setId(detail.getId());
            strategyContentMapper.updateByPrimaryKey(strategyContent);
            strategyDetailMapper.deleteRelation(detail.getId());
        }
        // tags
        if (tags == null) {
            return;
        }
        String[] tagStr = tags.split(",");
        for (String s : tagStr) {
            if (s != null) {
                StrategyTag strategyTag = new StrategyTag();
                strategyTag.setName(s);
                strategyTagService.saveOrUpdate(strategyTag);

                strategyDetailMapper.insertRelation(detail.getId(), strategyTag.getId());
            }
        }

    }

    @Override
    public StrategyContent getContentById(Long id) {
        if (id == null) {
            return null;
        }
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<StrategyDetail> queryDetailTop3(Long destinationId) {
        if (destinationId == null) {
            throw new LogicException("参数错误");
        }

        return strategyDetailMapper.selectDetailTop3(destinationId);
    }

    @Override
    public PageInfo<StrategyDetail> queryDetailForListByDestId(StrategyDetailQuery qo) {
        if (qo == null || qo.getDestId() == null) {
            throw new LogicException("参数错误");
        }
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        return new PageInfo<>(strategyDetailMapper.selectDetailForListByDestIdAndTagId(qo));
    }

    @Override
    public void dataPersistence(StrategyStatisPersistenceVO persistenceVO) {
        strategyDetailMapper.updateStatisVO(persistenceVO);
    }
}
